# -*- coding:utf-8 -*-
from .. import databaseDict
from PyQt5.QtCore import *

class MyAppDao(QObject):
    queryLimitedAppSignal = pyqtSignal(int, tuple)
    def __init__(self, appTablePanel):
        super(MyAppDao, self).__init__()
        self.db = databaseDict.DatabaseDict().dbDict['default']
        print("myAppDao:", id(self.db))
        self.appTablePanel = appTablePanel

    def queryLimitedAppsIntoTable(self, number, limit):
        def f(tx, pageNumber, recordLimit):
            sql = "select * from myapp limit %d, %d" % \
                  (pageNumber, recordLimit)
            rowCount = tx.execute(sql)
            allResult = tx.fetchall()
            self.queryLimitedAppSignal.emit(rowCount, allResult)
        #执行回调
        self.db.dbPool.runInteraction(f, number, limit)

    def queryTotalRecordCount(self):
        def f(tx):
            sql = "select count(*) from myapp"
            tx.execute(sql)
            totalRecordCount = tx.fetchone()
            print("totalRecordCount is ", totalRecordCount)
            self.appTablePanel.updateStatus(totalRecordCount[0])
        # 执行回调
        self.db.dbPool.runInteraction(f)

    def insertIntoOneRecord(self, recordTuple):
        def f(tx, record):
            sql = 'INSERT INTO myapp(app_name, update_time, app_status) VALUES (%s, %s, %s)'
            tx.execute(sql, record)
        # 执行回调
        self.db.dbPool.runInteraction(f, recordTuple)

    def delRecordByAppName(self, appName):
        def f(tx, appNameToDelete):
            sql = 'delete from myapp where app_name=%s'
            tx.execute(sql, appNameToDelete)
        #执行回调
        self.db.dbPool.runInteraction(f, appName)







